package com.qing.microservices.shirostudy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.qing.microservices.shirostudy.entity.Role;
import com.qing.microservices.shirostudy.entity.User;
import com.qing.microservices.shirostudy.entity.UserRole;
import com.qing.microservices.shirostudy.mapper.UserMapper;
import com.qing.microservices.shirostudy.service.IRoleService;
import com.qing.microservices.shirostudy.service.IUserRoleService;
import com.qing.microservices.shirostudy.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Q
 * @since 2023-06-06
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource
    private IRoleService roleService;

    @Override
    public User queryByUsername(String username) {
        // 查询账号
        return this.getOne(new QueryWrapper<User>().lambda().eq(User::getUsername, username));
    }

    @Override
    public List<Role> queryUserRoles(long userId) {
        MPJLambdaWrapper<Role> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(Role.class)
                .innerJoin(UserRole.class, UserRole::getRoleId, Role::getId)
                .eq(UserRole::getUserId, userId);
        return roleService.list(wrapper);
    }
}
